package com.changgou.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.changgou.business.dao.AdvertMapper;
import com.changgou.business.pojo.Advert;
import com.changgou.business.service.AdvertService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdvertServiceImpl implements AdvertService {

    @Autowired
    private AdvertMapper advertMapper;


    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Advert> findPage(Map<String,Object> searchMap, int page, int size) throws Exception {
        if(page<=0){
            page=1;
        }
        if(size<=0){
            size=10;
        }
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Advert> adverts = (Page<Advert>) advertMapper.selectByExample(example);
        List<Advert> result = adverts.getResult();
        for (Advert advert : result) {
            advert.setClicks(100);
            advert.setOrderData(50);
        }

        return adverts;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) throws Exception {
        Example example=new Example(Advert.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 广告名
            String name = (String) searchMap.get("name");
            if(name!=null && !"".equals(name)){
                criteria.andLike("name","%"+name+"%");
           	}
            // 广告位置
            String position = (String) searchMap.get("position");
            if(position!=null && !"".equals(position)){
                criteria.andEqualTo("position",position);
           	}
            // 到期时间
            String time = (String) searchMap.get("time");
            if(time!=null && time.length()>0) {
                int i=0;
                switch (time) {
                    case "1天内":
                         i = 1;
                        break;
                    case "3天内":
                         i = 3;
                        break;
                    case "1周内":
                         i = 7;
                        break;
                }
                LocalDateTime localDateTime = LocalDateTime.now();
                LocalDateTime nextlocalDateTime = localDateTime.minusDays(-i);
                criteria.andBetween("endTime",localDateTime,nextlocalDateTime);
            }
        }
        //必须上线才展示数据
        criteria.andEqualTo("status",1);
        return example;
    }

    /**
     * 查询广告位置
     * @return
     */
    public List<String> getAddress(){
        String address = advertMapper.getAddress();
        String[] strings = address.split(",");
        List list = new ArrayList();
        for (String string : strings) {
            String s = string.substring(string.lastIndexOf(":") + 1);
            list.add(s);
        }
        return list;
    }

    /**
     * 添加广告
     */
    @Override
    public void add(Advert advert) {
        advertMapper.insertSelective(advert);
    }

    @Override
    public void updateById(Integer id) {
        advertMapper.updateById(id);

    }

    @Override
    public void updateonTon(Integer id) {
        advertMapper.UpdateonTop(id);
    }

    /**
     * 根据id删除广告
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        advertMapper.deleteByPrimaryKey(id);
    }
}
